package com.jxs.android.sxyw;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.DangerDetailInfo;
import com.jxs.android.sxyw.data.DangerListInfo;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;

public class DangerListDetail extends AbstractActivity implements OnClickListener {

	private TextView danger_info_subject,// 主题
	        danger_info_grade_name// 隐患程度
	        , danger_info_business_name// 业务
	        , danger_info_city_country// 市县区
	        , danger_info_station_name// 站点
	        , danger_info_description// 描述
	        , danger_info_location// 坐标
	        , danger_info_photo// 照片
	        , danger_info_person// 上报人
	        , danger_info_updated// 上报时间
	        ;

	private LinearLayout danger_info_subject_ll, danger_info_business_name_ll, danger_info_grade_name_ll, danger_info_station_name_ll, danger_info_photo_ll,
	        danger_info_description_ll, danger_info_location_ll, danger_info_city_country_ll;
	private DangerListInfo dangerListInfo = null;
	private DangerDetailInfo noticeListInfo;
	private ProgressDialog progressDialog = null;

	private ArrayList<String> fileLists = new ArrayList<String>();

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.danger_detail);

		danger_info_subject = (TextView) findViewById(R.id.danger_info_subject);
		danger_info_grade_name = (TextView) findViewById(R.id.danger_info_grade_name);
		danger_info_business_name = (TextView) findViewById(R.id.danger_info_business_name);
		danger_info_city_country = (TextView) findViewById(R.id.danger_info_city_country);
		danger_info_station_name = (TextView) findViewById(R.id.danger_info_station_name);
		danger_info_location = (TextView) findViewById(R.id.danger_info_location);
		danger_info_description = (TextView) findViewById(R.id.danger_info_description);
		danger_info_updated = (TextView) findViewById(R.id.danger_info_updated);
		danger_info_photo = (TextView) findViewById(R.id.danger_info_photo);
		danger_info_person = (TextView) findViewById(R.id.danger_info_person);

		danger_info_subject_ll = (LinearLayout) findViewById(R.id.danger_info_subject_ll);
		danger_info_grade_name_ll = (LinearLayout) findViewById(R.id.danger_info_grade_name_ll);
		danger_info_photo_ll = (LinearLayout) findViewById(R.id.danger_info_photo_ll);
		danger_info_station_name_ll = (LinearLayout) findViewById(R.id.danger_info_station_name_ll);
		danger_info_description_ll = (LinearLayout) findViewById(R.id.danger_info_description_ll);
		danger_info_location_ll = (LinearLayout) findViewById(R.id.danger_info_location_ll);
		danger_info_business_name_ll = (LinearLayout) findViewById(R.id.danger_info_business_name_ll);
		danger_info_city_country_ll = (LinearLayout) findViewById(R.id.danger_info_city_country_ll);
		danger_info_grade_name_ll.setOnClickListener(this);
		danger_info_photo_ll.setOnClickListener(this);
		danger_info_subject_ll.setOnClickListener(this);
		danger_info_station_name_ll.setOnClickListener(this);
		danger_info_description_ll.setOnClickListener(this);
		danger_info_location_ll.setOnClickListener(this);
		danger_info_business_name_ll.setOnClickListener(this);
		danger_info_city_country_ll.setOnClickListener(this);

		if (null != getIntent() && null != getIntent().getExtras()) {
			dangerListInfo = (DangerListInfo) getIntent().getExtras().getSerializable("dangerListInfo");
			progressDialog = ProgressDialog.show(this, "", "正在加载，请稍候...", true);
			progressDialog.setCancelable(true);
			getNoticeDetail(SelfApplication.getUserInfo().getTicket(), dangerListInfo.getDanger_id());
		}

		danger_info_person.setText(SelfApplication.getUserInfo().getUser_name());
		if (null != dangerListInfo) {
			danger_info_subject.setText(dangerListInfo.getSubject());
		}

	}

	private int GET_NEW = 0x0091;
	private int ERROR_MSG = 0x0092;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (msg.what == GET_NEW) {
				noticeListInfo = (DangerDetailInfo) msg.obj;
				danger_info_subject.setText(noticeListInfo.getSubject());
				danger_info_grade_name.setText(noticeListInfo.getGrade_name());
				danger_info_business_name.setText(noticeListInfo.getBusiness_name());
				danger_info_city_country.setText(noticeListInfo.getCity_name() + " " + noticeListInfo.getCounty_name());
				danger_info_station_name.setText(noticeListInfo.getStation_name());
				danger_info_location.setText("纬度：" + noticeListInfo.getLatitude() + ",经度：" + noticeListInfo.getLongitude());
				danger_info_description.setText(noticeListInfo.getDescription());
				danger_info_updated.setText(noticeListInfo.getUpdated());
				if (fileLists.size() != 0) {
					danger_info_photo.setText(fileLists.size() + "张照片");
				} else {
					danger_info_photo.setText("无");
				}
			} else if (msg.what == ERROR_MSG) {
				PopMsg.show(msg.obj.toString());
			}
		}

	};

	private void getNoticeDetail(final String ticket, final String danger_id) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "getHiddenDangerByID";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("danger_id", danger_id);
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser(response);
					}
				});
			}
		}).start();
	}

	private void paser(SoapObject soapObject) {
		if (null != soapObject) {
			String stauts = soapObject.getPropertySafelyAsString("status");
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getPropertySafelyAsString("message");
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
				StartHelper.startActivity(Login.class);
			} else if ("0".equals(stauts)) {
				SoapObject dangerDetailObject = (SoapObject) soapObject.getProperty("retMsg");
				if (null != dangerDetailObject) {
					DangerDetailInfo dangerDetailInfo = new DangerDetailInfo();

					dangerDetailInfo.setCreate_order_flag(dangerDetailObject.getPropertySafelyAsString("create_order_flag"));// 状态[Y;//'已处理',N;//'未处理']
					dangerDetailInfo.setDanger_id(dangerDetailObject.getPropertySafelyAsString("danger_id"));// 隐患编号
					dangerDetailInfo.setGrade_name(dangerDetailObject.getPropertySafelyAsString("grade_name"));// 隐患程度
					dangerDetailInfo.setSubject(dangerDetailObject.getPropertySafelyAsString("subject"));// 主题
					dangerDetailInfo.setUpdated(dangerDetailObject.getPropertySafelyAsString("updated"));// 上报时间
					dangerDetailInfo.setDescription(dangerDetailObject.getPropertySafelyAsString("description"));// 描述
					dangerDetailInfo.setLatitude(dangerDetailObject.getPropertySafelyAsString("latitude"));// 维度
					dangerDetailInfo.setLongitude(dangerDetailObject.getPropertySafelyAsString("longitude"));// 精度
					dangerDetailInfo.setBusiness_name(dangerDetailObject.getPropertySafelyAsString("business_name"));// 业务名称
					dangerDetailInfo.setCity_name(dangerDetailObject.getPropertySafelyAsString("city_name"));// 地市名称
					dangerDetailInfo.setCounty_name(dangerDetailObject.getPropertySafelyAsString("county_name"));// 县区名称
					dangerDetailInfo.setStation_name(dangerDetailObject.getPropertySafelyAsString("station_name"));// 站点名称

					initPic((SoapObject) dangerDetailObject.getProperty("fileList"));
					Message msg = new Message();
					msg.what = GET_NEW;
					msg.obj = dangerDetailInfo;
					handler.sendMessage(msg);
				} else {
					Message msg = new Message();
					msg.what = ERROR_MSG;
					msg.obj = message;
					handler.sendMessage(msg);
				}
			}
		}
	}

	private void initPic(SoapObject fileListSoapObject) {
		if (null != fileListSoapObject) {
			fileLists = new ArrayList<String>();
			for (int i = 0; i < fileListSoapObject.getPropertyCount(); i++) {
				SoapObject fileBeanObject = (SoapObject) fileListSoapObject.getProperty(i);
				fileLists.add(fileBeanObject.getPropertySafelyAsString("upload_file_url"));
			}
		}

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == danger_info_photo_ll.getId()) {
			if (fileLists.size() > 0) {
				if (fileLists.size() == 1) {
					Intent intent = new Intent(this, ViewPicWeb.class);
					Bundle bundle = new Bundle();
					bundle.putString("url", fileLists.get(0));
					intent.putExtras(bundle);
					startActivity(intent);
				} else {
					Intent intent = new Intent(this, ViewPicGrid.class);
					Bundle bundle = new Bundle();
					bundle.putStringArrayList("fileLists", fileLists);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			} else {
				PopMsg.show("无图片可查看...");
			}
		} else if (v.getId() == danger_info_subject_ll.getId()) {
			if (null != dangerListInfo) {
				requestDesDetail("主题", dangerListInfo.getSubject());
			}
		} else if (v.getId() == danger_info_station_name_ll.getId()) {
			if (null != dangerListInfo)
				requestDesDetail("站点名称", noticeListInfo.getStation_name());
		} else if (v.getId() == danger_info_description_ll.getId()) {
			if (null != dangerListInfo)
				requestDesDetail("描述信息", noticeListInfo.getDescription());
		} else if (v.getId() == danger_info_location_ll.getId()) {
			if (null != dangerListInfo) {
				Intent intent = new Intent(this, StatecraftDetail.class);
				Bundle bundle = new Bundle();
				bundle.putString("longitude", noticeListInfo.getLongitude());
				bundle.putString("latitude", noticeListInfo.getLatitude());
				bundle.putString("elevation", "");
				intent.putExtras(bundle);
				startActivity(intent);
			}
		}
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindBtn();
		topBar.setTitle(R.string.danger_list_detail);
		initTopBarReturn(topBar);
	}

}
